<?php

require_once "../common/login.php";

$string_winner = "//table[@class='electiondetails largepadded']";
$string_loser = "//table[@class='electiondetails']";

$string_name = ".//div[@class='avatarholder']/a";
$string_party = "./td[3]/a/span";
$string_votes = "./td[4]";

$username = "usher@21cn.com";
$userpass = "flyingfish";

$id_file = 'region.txt';

$total_seats = array();
$total_votes = array();

function login($name, $passwd)
{
	global $link;

	if ( $link->login($name, $passwd) == TRUE ) {
		echo "Login succeed.\r\n";
	} else {
		die("Login failed.\r\n");
	}
}


function do_parse($query, $xpath, $countable)
{
	global $string_name, $string_party, $string_votes;
	global $total_seats, $total_votes;

	$result = '';
	
	$elements = $xpath->query($query);

	foreach($elements as $element) {
		$item = $element->firstChild;
		while ($item = $item->nextSibling) {

			$entries = $xpath->query($string_name, $item);
			$name = $entries->item(0)->getAttribute('title');

			$entries = $xpath->query($string_party, $item);
			$party = $entries->item(0)->textContent;
			
			$entries = $xpath->query($string_votes, $item);
			$votes = $entries->item(0)->textContent;

			if (!array_key_exists($party, $total_votes)) {
					$total_votes[$party] = 0;
			}
			$total_votes[$party] += $votes;
			
			if ($countable == TRUE ) {
				if (!array_key_exists($party, $total_seats)) {
					$total_seats[$party] = 0;
				}
				$total_seats[$party] += 1;
			}

			echo "$name, $party, $votes\r\n";
			if ($countable == TRUE ) {
				if ($party == "Saturn Community") {
					$result .= "<span style='color:blue'>";
				} else {
					$result .= "<span style='color:red'>";
				}
			}

			$result .= "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp $name, ($party), $votes";

			if ($countable == TRUE ) {
				$result .= "</span>";
			}

			$result .=  "<br/>\r\n";
		}
	}
	
	return $result;
}


function Parse($rid,$rname)
{
	global $string_winner, $string_loser;
	global $link;

	$result = '';
	
	echo "$rname\r\n";
	$result .= "$rname<br/>\r\n";
	
	$filename = "http://www.erepublik.com/en/elections/country-14-election-congress-0-date-2011-05-region-$rid.htm";

	$file = $link->getPage($filename);

	$doc = new DOMDocument();
	$doc->loadHTML($file);
	$xpath = new DOMXpath($doc);

	$result .= do_parse($string_winner, $xpath, TRUE);
	$result .= do_parse($string_loser, $xpath, FALSE);
	
	$result .= "--------------<br/>\r\n";
	
	return $result;
}

//main
$lines = file($id_file, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);

$content = '';

$link = new loginObj();

login($username, $userpass);

libxml_use_internal_errors(true);

foreach($lines as $line) {
	$a = explode(',', trim($line));
	$content .= Parse($a[0], $a[1]);
}

$mtime = time();
date_default_timezone_set('Asia/Shanghai');
$modified = date("Y.m.d H:i", $mtime);

$header = "Total Seats:<br/><br/>\r\n";
foreach($total_seats as $key => $val) {
	$header .= "$key: $val (" . $total_votes[$key] . ")<br/>\r\n";
}

$content = "<html><body>\r\n<span style='color:blue'>Updated: $modified</span><br/><br/>\r\n" . $header  . "<br/>\r\n" . $content . "</body></html>";

$file = fopen('result.html', 'w');
fwrite($file, $content);
fclose($file);

?>